import simpy
from core.board import Board
from core.scheduler import Scheduler
from core.broker import Broker
from core.simulation import Simulation
from core.task import Task
import pandas as pd

class Episode(object):
    def __init__(self, worker_configs, plane_configs, algorithm):
        env = simpy.Environment()
        self.env = env
        schedule_events = [env.event()]
        # todo: schedule_event important part in simulation
        board = Board()
        board.add_workers(worker_configs=worker_configs)
        # 调度事件的使用者/生
        plane_broker = Broker(self.env,schedule_events, plane_configs)
        scheduler = Scheduler(self.env,schedule_events,algorithm)
        # 調度事件產生
        Task.event_init(schedule_events)

        self.simulation = Simulation(self.env, board, plane_broker, scheduler, None)

    def log_gantt(self) -> pd.DataFrame:

         gantt_list = Task.log_gantt()
         data = pd.DataFrame(data=gantt_list,columns=['task_type','node_id','plane_id',\
                               'worker_id','start','end'])

         return data

    def run(self):
        self.simulation.run()
        self.env.run()
